package view.render.monster
{
	import enums.main.UISize;
	
	import feathers.controls.Label;
	import feathers.controls.List;
	import feathers.controls.Scroller;
	import feathers.controls.renderers.DefaultListItemRenderer;
	import feathers.controls.renderers.IListItemRenderer;
	import feathers.data.ListCollection;
	
	import model.vo.ItemVo;
	import model.vo.monster.LMonsterCollectVo;
	
	import starling.events.Event;
	import starling.text.TextField;
	import starling.utils.Color;
	
	import view.component.monster.large_monster.LMDetailCollectView;
	import view.event.ItemEvent;
	import view.render.BaseItemScrollerItem;
	
	
	/**
	 * 大型怪物-剥取掉落列表item
	 * @author £iang
	 * 
	 */	
	public class LMonsterCollectListItem extends BaseItemScrollerItem
	{
		//-----------------------------
		// variables
		//-----------------------------
		
		/** 次数信息 */
		public var timesLabel:Label;
		/** 下位道具信息 */
		public var lowList:List;
		/** 上位道具信息 */
		public var highList:List;
		/** 条件信息 */
		public var conditionLabel:TextField;
		
		//-----------------------------
		// properties
		//-----------------------------
		
		/** lowList数据 */
		private var _listDataLow:ListCollection;
		/** highList数据 */
		private var _listDataHigh:ListCollection;
		
		//-----------------------------
		// setter&getter
		//-----------------------------
		
		//-----------------------------
		// methods
		//-----------------------------
		
		/**
		 * 初始化函数 
		 * 
		 */		
		override protected function initializeOther():void
		{
			super.initializeOther();
			timesLabel = new Label();
			scollerContainer.addChild( timesLabel );
			
			lowList = new List();
			scollerContainer.addChild( lowList );
			lowList.width = UISize.MAIN_W * 0.6;
			lowList.itemRendererProperties.labelField  = "label";
			lowList.itemRendererProperties.iconField = "icon";
			lowList.itemRendererFactory = itemLowRenderFactroy;
			lowList.hasElasticEdges = false;
			lowList.scrollerProperties.scrollBarDisplayMode = Scroller.SCROLL_BAR_DISPLAY_MODE_NONE;
			
			highList = new List();
			scollerContainer.addChild( highList );
			highList.width = UISize.MAIN_W * 0.6;
			highList.itemRendererProperties.labelField  = "label";
			highList.itemRendererProperties.iconField = "icon";
			highList.itemRendererFactory = itemHighRenderFactroy;
			highList.hasElasticEdges = false;
			highList.scrollerProperties.scrollBarDisplayMode = Scroller.SCROLL_BAR_DISPLAY_MODE_NONE;
			
			conditionLabel = new TextField(
				UISize.MAIN_W * 0.75, 
				_itemH * UISize.DPI_SCALE,
				"", "微软雅黑,宋体", 24 * UISize.DPI_SCALE, Color.WHITE
			);
			conditionLabel.vAlign = "center";
			conditionLabel.hAlign = "left";
			scollerContainer.addChild( conditionLabel );
		}
		
		/**
		 * 调整组件 
		 * 
		 */		
		override protected function draw():void
		{
			var labelY:Number = ( _itemH - 43 ) / 2 * UISize.DPI_SCALE;
			var leftPadding:Number = UISize.TITLE_PADDING_LEFT * UISize.DPI_SCALE;
			var firstLabelFullW:Number;
			
			firstLabel.x = leftPadding;
			firstLabelFullW = UISize.MAIN_W * _firstPercent;
			
			timesLabel.y = labelY;
			timesLabel.x = 0;
			timesLabel.width  = UISize.MAIN_W * 0.15;
			
			lowList.x = UISize.MAIN_W * 0.15;
			highList.x = UISize.MAIN_W * 0.75;
			
			conditionLabel.x = UISize.MAIN_W * 1.5;
			conditionLabel.width = UISize.MAIN_W * 0.75;
			
			scollerContainer.x = firstLabelFullW;
			scollerContainer.height = _itemH * UISize.DPI_SCALE;
			scollerContainer.width = UISize.MAIN_W - firstLabelFullW;
			
			this.width = UISize.MAIN_W;
			this.height = _itemH * UISize.DPI_SCALE;
		}
		
		/**
		 * 设置item数据 
		 * @param data 数据
		 * 
		 */		
		public function setData( data:LMonsterCollectVo ):void
		{
			firstLabel.text = data.part;
			timesLabel.text = data.times;
			conditionLabel.text = data.condition;
			
			var len:int;
			var i:int;
			var tmpH:Number;
			
			_listDataLow = new ListCollection([]);
			len = data.low.length;
			
			for ( i = 0; i < len; i ++ )
			{
				_listDataLow.push( mapItemVo2ListObj( data.low[ i ] as ItemVo ) );
			}
			
			lowList.height = tmpH = UISize.ITEM_LIST_ITEM_H * 
				UISize.DPI_SCALE * len;
			lowList.y = ( _itemH * UISize.DPI_SCALE - tmpH ) / 2;
			lowList.dataProvider = _listDataLow;
			
			_listDataHigh = new ListCollection([]);
			len = data.high.length;
			
			for ( i = 0; i < len; i ++ )
			{
				_listDataHigh.push( mapItemVo2ListObj( data.high[ i ] as ItemVo ) );
			}
			
			highList.height = tmpH = UISize.ITEM_LIST_ITEM_H * 
				UISize.DPI_SCALE * len;
			highList.y = ( _itemH * UISize.DPI_SCALE - tmpH ) / 2;
			highList.dataProvider = _listDataHigh;
		}
		
		/**
		 * itemLowRenderer皮肤 
		 * @return 
		 * 
		 */		
		private function itemLowRenderFactroy():IListItemRenderer
		{
			var renderer:DefaultListItemRenderer = commonItem();
			renderer.addEventListener( Event.TRIGGERED, itemLowTriggeredHandler );
			return renderer;
		}
		
		/**
		 * itemHighRenderer皮肤 
		 * @return 
		 * 
		 */		
		private function itemHighRenderFactroy():IListItemRenderer
		{
			var renderer:DefaultListItemRenderer = commonItem();
			renderer.addEventListener( Event.TRIGGERED, itemHighTriggeredHandler );
			return renderer;
		}
		
		
		//-----------------------------
		// events
		//-----------------------------
		
		/**
		 * 处理下位道具item被点击后派发的事件 
		 * @param event 道具item被点击后派发的事件 
		 * 
		 */		
		protected function itemLowTriggeredHandler( event:Event ):void
		{
			var itemId:int =  _listDataLow.data[ ( event.target as DefaultListItemRenderer ).index ].itemId;	
			var itemEvent:ItemEvent = new ItemEvent( ItemEvent.SEARCH_ITEM );
			
			itemEvent.curSearchItemId = itemId;
			( this.parent.parent.parent.parent as LMDetailCollectView ).dispatchCommonEvent( itemEvent );
		}
		
		/**
		 * 处理上位道具item被点击后派发的事件 
		 * @param event 道具item被点击后派发的事件 
		 * 
		 */		
		protected function itemHighTriggeredHandler( event:Event ):void
		{
			var itemId:int = _listDataHigh.data[ ( event.target as DefaultListItemRenderer ).index ].itemId;	
			var itemEvent:ItemEvent = new ItemEvent( ItemEvent.SEARCH_ITEM );
			
			itemEvent.curSearchItemId = itemId;
			( this.parent.parent.parent.parent as LMDetailCollectView ).dispatchCommonEvent( itemEvent );
		}
		
		//-----------------------------
		// dispose
		//-----------------------------
		
		/**
		 * 析构函数 
		 * 
		 */		
		override protected function disposeAnythingInContainer():void
		{
			disposeLabels( [
				"times", "condition"
			] );
			
			disposeItemListData( _listDataLow );
			_listDataLow = null;
			
			lowList.dispose();
			lowList = null;
			
			disposeItemListData( _listDataHigh );
			_listDataHigh = null;
			
			highList.dispose();
			highList = null;
		}
		
		//-----------------------------
		// construction
		//-----------------------------
		
		/**
		 * 构造函数
		 *
		 */
		public function LMonsterCollectListItem()
		{
			super();
			_itemH = UISize.LMONSTER_COLLECT_ITEM_H;
			_firstPercent = 0.25;
			initialize();
		}
	}
}